package com.cj.ctyl.service.impl;

import com.cj.ctyl.common.Md5SaltUtils;
import com.cj.ctyl.entity.SysUsers;
import com.cj.ctyl.mapper.SysUserMapper;
import com.cj.ctyl.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: Alex
 * @Description: 用户
 * @Date: 2021/8/14
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUsers selectSysUserById(Long id) {
        return sysUserMapper.selectSysUserById(id);
    }

    @Override
    public SysUsers selectSysUser(SysUsers sysUsers) {
        return sysUserMapper.selectSysUser(sysUsers);
    }

    @Override
    public List<SysUsers> selectSysUserList(SysUsers sysUsers) {
        return sysUserMapper.selectSysUserList(sysUsers);
    }

    @Override
    public int insertSysUser(SysUsers sysUsers) {
        Map<String, String> md5Encryption = Md5SaltUtils.getMD5Encryption(sysUsers.getPassword());
        sysUsers.setPassword(md5Encryption.get("md5"));
        sysUsers.setSalt(md5Encryption.get("salt"));
        return sysUserMapper.insertSysUser(sysUsers);
    }

    @Override
    public int updateSysUser(SysUsers sysUsers) {
        return sysUserMapper.updateSysUser(sysUsers);
    }

    @Override
    public SysUsers login(String username, String password) {
        SysUsers sysUsers = new SysUsers();
        sysUsers.setAccount(username);
        SysUsers sysUsers1 = sysUserMapper.selectSysUser(sysUsers);
        if (sysUsers1 != null && Md5SaltUtils.comparison(password, sysUsers1.getPassword(), sysUsers1.getSalt())) {
            return sysUsers1;
        }
        return null;
    }
}
